package content.views.works;
import feffects.easing.Quad;
import js.Lib;
import tools.ASprite;

/**
 * ...
 * @author PRASS
 */

class MiniNav extends ASprite
{
	
	public var midY		: Int;
	public var endY		: Int;
	
	private var _top 		: MiniNavItem;
	private var _mid 		: MiniNavItem;
	private var _bottom 	: MiniNavItem;
	
	private var _current	: MiniNavItem;
	
	private var _layer		: ASprite;
	private var _isDown		: Bool;
	private var _isTweening : Bool;
	private var _wheelActive:Bool;
	
	private var _unActiveScreenShots : Dynamic;
	
	public function new( jq : JQuery, layer : ASprite, _cb : Dynamic) 
	{
		super(jq);
		
		_unActiveScreenShots = _cb;
		_layer = layer;
		
		var html : String = '	<span id="btTop" 	class="miniNavItem">1</span>
								<span id="btMid" 	class="miniNavItem">2</span>
								<span id="btBottom" class="miniNavItem">3</span>';
		
		dom.append(html);
		
		_top 	= new MiniNavItem( new JQuery("#btTop"),"btTop");
		_mid 	= new MiniNavItem( new JQuery("#btMid"),"btMid");
		_bottom = new MiniNavItem( new JQuery("#btBottom"),"btBottom");
		
		dom.mousedown( function(e) { _update(e); } );
		
		_current = _top;
		_current.select();
		
		untyped{ if (window.addEventListener) window.addEventListener('DOMMouseScroll', this._wheel, false); }
		untyped{ if (window.addEventListener) window.addEventListener('mousewheel', this._wheel, false); }
		
		if(Lib.isIE || Lib.isOpera) untyped { window.onmousewheel = document.onmousewheel = this._wheel;	}
		
		setAlpha(0);
	}
	
	public function display() : Void
	{
		tween(0, setAlpha, 1, 600, Quad.easeInOut, null, 100);
		_wheelActive = true;
		
	}
	
	public function hide() : Void
	{
		tween(1, setAlpha, 0, 500, Quad.easeInOut);
		_wheelActive = false;
	}
	
	
	private function _update(e) : Void
	{
		if ( e.target.id == _current.domID || _isTweening ) return;
		
		_isTweening = true;
		
		//_unActiveScreenShots(false);
		
		_current.unselect();
		
		var value : Int = 0;
		
		switch( e.target.id )
		{
			case "btTop":
				_isDown = false;
				_current = _top;
			case "btMid":
				_isDown = true;
				_current = _mid;
				value = midY;
			case "btBottom":
				_isDown = true;
				_current = _bottom;
				value = endY;
		}
		
		_move(value);
	}
	
	private function _move ( value : Int ) : Void
	{
		tween( _layer.getY(), _layer.setY, value, 600, Quad.easeInOut, _clear);
		_current.select();
	}
	
	private function _clear() : Void
	{
		_isTweening = false;
	}
	
	public function reInit() : Void
	{
		if (!_isDown) return;
		
		_isDown = false;
		_isTweening = false;
		_current.unselect();
		
		_current = _top;
		tween( _layer.getY(), _layer.setY, 0, 600, Quad.easeInOut, _clear);
		_current.select();
		
		if ( getAlpha() < 1) display();
	}
	
	/* Wheel */
	private function _wheel(event) : Void
	{
		if (!_wheelActive || _isTweening ) return;
		
		var delta = 0;
        untyped{ if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail / 3;
        } }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta != 0) _handle(delta);
			
        // safety
        untyped{ if (event.preventDefault)
                event.preventDefault();
				
		event.returnValue = false; }
	}
	
	
	private function _handle(delta) : Void
	{
		_isTweening = true;
		_current.unselect();
		var value : Int = 0;
		
        if (delta > 0)
		{
			switch( _current.domID )
			{
				case "btTop":
					_current.select();
					_isTweening = false;
					return;
				case "btMid":
					_isDown = false;
					_current = _top;
				case "btBottom":
					_isDown = true;
					_current = _mid;
					value = midY;
			}
			
		} else {
			
			switch( _current.domID )
			{
				case "btTop":
					_isDown = true;
					_current = _mid;
					value = midY;	
				case "btMid":
					_isDown = true;
					_current = _bottom;
					value = endY;
				case "btBottom":
					_current.select();
					_isTweening = false;
					return;
			}
        }
		
		_move(value);
	}



	
}